/*
 * @LastEditors: w
 */
// 升级者
import {uid} from "../utils";

export default {
    create(name){
        // console.log(JSON.stringify(Game.spawns['Spawn1'].store))
        Game.spawns['Spawn1'].spawnCreep( [WORK, WORK,CARRY, MOVE], name ,{
            memory: {role: 'upgrader'}
        });
        // Game.creeps[name].memory.role = 'upgrader';
    },
    update(creep){
        this.run(creep)
        if(Game.spawns['Spawn1'].store.energy==300){
            console.log('新建了upgrader','名字为：','upgrader'+uid())
            this.create('upgrader'+uid())
        }
    },
    run(creep) {
        // console.log(creep.store[RESOURCE_ENERGY] , creep.store.getCapacity())
        // if(creep.store.getFreeCapacity() > 0 ) {
        //     creep.say('去吸取能量'+creep.store[RESOURCE_ENERGY]+'/'+creep.store.getCapacity());
        //     // console.log(5555555)
        //     var sources = creep.room.find(FIND_SOURCES);
        //     if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
        //         creep.moveTo(sources[0]);
        //     }
        // }
        // else {
        //     // console.log('aaaa',creep.store.getFreeCapacity(RESOURCE_ENERGY) )
        //     if(creep.transfer(creep.room.controller, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
        //         creep.moveTo(creep.room.controller);
        //     }
        // }


        console.log(creep.store[RESOURCE_ENERGY])
        if(creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) {
            creep.memory.upgrading = false;
            creep.say('🔄 harvest');
        }
        if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) {
            creep.memory.upgrading = true;
            creep.say('⚡ upgrade');
        }

        if(creep.memory.upgrading) {
            if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
                creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
            }
        }
        else {
            var sources = creep.room.find(FIND_SOURCES);
            if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
                creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
            }
        }
    }
}